﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace BizTalk.ESB.Extensions.Helpers
{
    [Serializable]
    public class SerializationHelpers
    {
        public static XmlDocument SerializeAnObject(object someObject)
        {
            XmlSerializer serializer = new XmlSerializer(someObject.GetType());
            StringWriter writer = new StringWriter();
            serializer.Serialize(writer, someObject);
            XmlDocument response = new XmlDocument();
            response.LoadXml(writer.ToString());
            return response;
        }

        public static Object DeSerializeAnObject(XmlDocument xmlDoc, Type type)
        {
            StringReader stringReader = new StringReader(xmlDoc.OuterXml);
            XmlSerializer serializer = new XmlSerializer(type);
            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader);
            try
            {
                Object someObject = serializer.Deserialize(xmlReader);
                return someObject;
            }
            finally
            {
                xmlReader.Close();
                stringReader.Close();
            }
        }
    }
}
